package com.hducms.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hducms.cms.service.CmsRoleService;
import com.hducms.cms.dao.CmsRoleDao;
//import com.hducms.cms.dao.main.CmsRoleDao;
import com.hducms.cms.entity.main.CmsRole;
//import com.hducms.cms.manager.main.CmsRoleMng;
//import com.hducms.common.hibernate3.Updater;

@Service
@Transactional
public class CmsRoleServiceImpl implements CmsRoleService {
	@Transactional(readOnly = true)
	public List<CmsRole> getList() {
		return dao.getList();
	}

	@Transactional(readOnly = true)
	public CmsRole findById(Integer id) {
		CmsRole entity = dao.findById(id);
		return entity;
	}

	public CmsRole save(CmsRole bean, Set<String> perms) {
		dao.save(bean);
		int id = bean.getRoleId();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", perms);
		map.put("id", id);
		
		dao.insertPermission(map);
		//bean.setPerms(perms);
		//
		return bean;
	}

	public CmsRole update(CmsRole bean, Set<String> perms) {
		dao.update(bean);
		dao.deletePermissionById(bean.getRoleId());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", perms);
		map.put("id", bean.getRoleId());
		dao.insertPermission(map);
		return bean;
	}

	public void deleteById(Integer id) {
		dao.deletePermissionById(id);
		dao.deleteRoleById(id);
	}

	public void deleteByIds(Integer[] ids) {
		for (int i = 0, len = ids.length; i < len; i++) {
			deleteById(ids[i]);
		}
	}

	private CmsRoleDao dao;

	@Autowired
	public void setDao(CmsRoleDao dao) {
		this.dao = dao;
	}
}